package com.tavenli.interceptors;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class ActiveUserListener implements HttpSessionListener{
	private static int sessionCount = 0;
	private static Map<String, HttpSession> sessionMaps = new HashMap<String, HttpSession>(); // 存放session的集合类
	public void sessionCreated(HttpSessionEvent arg0) {
		HttpSession session = arg0.getSession();
		String sessionId = session.getId();
		System.out.println("Create a session:" + sessionId);
		sessionMaps.put(sessionId, session);
		sessionCount++;
	}

	public void sessionDestroyed(HttpSessionEvent arg0) {
		sessionCount--;
		String sessionId = arg0.getSession().getId();
		sessionMaps.remove(sessionId);// 利用会话ID标示特定会话
		System.out.println("Destroy a session:" + sessionId);
	}

	public static int getSessionCount() {
		return sessionCount;
	}

	public static Map<String, HttpSession> getSessionMaps() {
		return sessionMaps;
	}
}
